package com.googlecode.monitoria.client.gui;

import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormButtonBinding;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.googlecode.monitoria.client.Application;
import com.googlecode.monitoria.client.services.MonitoriaServiceAsync;
import com.googlecode.monitoria.client.to.UserTO;

/**
 * Login screen.
 * @author Ricardo Araújo Santos - ricardo@lsd.ufcg.edu.br
 */
public class LoginScreen extends LayoutContainer{
	
	/**
	 * {@inheritDoc}
	 */
	@Override
	protected void onRender(Element parent, int index) {
		super.onRender(parent, index);
		setLayout(new CenterLayout());
		
		FormData formData = new FormData();
		
		final FormPanel simple = new FormPanel();  
	    simple.setHeading("Login");  
	    simple.setFrame(true);  
	    simple.setWidth(350);
	    
	    final TextField<String> login = new TextField<String>();  
	    login.setFieldLabel("Login");  
	    login.setAllowBlank(false);  
	    login.setData("aria-previous", simple.getButtonBar().getId());  
	    simple.add(login, formData);  
	    
	    final TextField<String> password = new TextField<String>();  
	    password.setFieldLabel("Password");  
	    password.setPassword(true);
	    password.setAllowBlank(false);  
	    password.setData("aria-previous", simple.getButtonBar().getId());
	    password.addKeyListener(new KeyListener(){
	    	@Override
	    	public void componentKeyPress(ComponentEvent event) {
	    		if(event.getKeyCode() == KeyCodes.KEY_ENTER){
	    			if(!simple.isValid()){
						return;
					}
					checkLogin(login.getValue(), password.getValue());
	    		}
	    	}	    	
	    });
	    simple.add(password, formData); 
	    
	    Button b = new Button("Login");
	    
	    b.addSelectionListener(new SelectionListener<ButtonEvent>() {
			
			@Override
			public void componentSelected(ButtonEvent ce) {
				if(!simple.isValid()){
					return;
				}
				checkLogin(login.getValue(), password.getValue());
			}
		});
	    
	    simple.addButton(b);

	    FormButtonBinding binding = new FormButtonBinding(simple);  
	    binding.addButton(b); 
	    
	    
		add(simple);
	}
	
	/**
	 * Attempts to log the user in using a given login and password. 
	 * @param login User's login
	 * @param password User's password.
	 */
	protected void checkLogin(String login, String password) {
		MonitoriaServiceAsync.Util.getInstance().login(login, password, new AsyncCallback<UserTO>() {
			public void onSuccess(UserTO user) {
				if(user != null){
					setErrorText("");
					Application.get().setHomeScreen(user);
				}else{
					setErrorText("Invalid name or password!");
				}
			}
			public void onFailure(Throwable ex) {
				setErrorText("Error: " + ex.getMessage());
			}
		});
	}
	
	/**
	 * Set error message.
	 * @param message Error message.
	 */
	protected void setErrorText(String message) {
		System.out.println(message);
	}
}
